<div style="margin: 5px;">
    <div class="layui-btn-group userTable">
        <button class="layui-btn" data-type="addSort">添加书籍</button>
    </div>
    <table id="books-table" lay-filter="books-table-filter"></table>
</div>

<script type="text/html" id="booksBar">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <a class="layui-btn  layui-btn-xs" lay-event="edit">修改</a>
</script>
<script type="text/html" id="user-header-tpl">
        <img style="width: auto;height: auto;max-width: 100%;max-height: 100%" src="{{d.book_img}}" class="">
</script>

<script type="text/html" id="time-tpl">
    {{timestampToTime(d.creat_time)}}
</script>

<script>
    function timestampToTime(timestamp) {
        var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
        Y = date.getFullYear() + '-';
        M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
        D = date.getDate() + ' ';
        h = date.getHours() + ':';
        m = date.getMinutes() + ':';
        s = date.getSeconds();
        return Y+M+D+h+m+s;
    }
    layui.use(['table', 'form'], function () {
        var table = layui.table;
        //-------会员管理表格s--------
        var user = table.render({
            elem: '#books-table'
            , height: 'full-180'
            , url: '/admin/books/ajax'
            , page: true
            , cols: [[
                {field: 'book_id', title: 'ID',align:'center'}
                , {field: 'book_img', title: '封面',align:'center',templet:"#user-header-tpl"}
                , {field: 'book_name', title: '名称',align:'center'}
                , {field: 'book_auth', title: '作者',align:'center'}
                , {field: 'book_man', title: '出版社',align:'center'}
                , {field: 'book_dec', title: '描述',align:'center'}
                , {field: 'book_num', title: '数量',align:'center'}
                , {field: 'creat_time', title: '上架时间',templet:'#time-tpl',align:'center'}
                , {field: '', title: '操作', toolbar: '#booksBar',align:'center'}
            ]]
        });
        table.on('tool(books-table-filter)', function (obj) {
            let data = obj.data;
            if (obj.event == 'del') {
                layer.confirm('是否要删除' + data.book_name + '这本书', function (index) {
                    $.ajax({
                        type: 'post'
                        , url: '/admin/books/dell'
                        , dataType: 'json'
                        , data: {book_id: data.book_id}
                        , success: function (obj1) {
                            if (obj1.code == 1) {
                                obj.del();
                            }
                            layer.alert(obj1.msg);
                        }
                    });
                    layer.close(index);
                });
            } else if (obj.event == 'edit') {
                layer.open({
                    type: 2 //Page层类型
                    , area: ['550px', '600px']
                    , title: '修改书籍'
                    , shade: 0.6 //遮罩透明度
                    , maxmin: true //允许全屏最小化
                    , anim: 1 //0-6的动画形式，-1不开启
                    , content: '/admin/books/edit?book_id=' + data.book_id
                    , end: function () {
                        user.reload('user-table');

                    }
                });
            }
        });
        var $ = layui.$, active = {
            addSort: function () { //获取选中数据
                layer.open({
                    type: 2 //Page层类型
                    , area: ['550px', '700px']
                    , title: '添加书籍'
                    , shade: 0.6 //遮罩透明度
                    , maxmin: true //允许全屏最小化
                    , anim: 1 //0-6的动画形式，-1不开启
                    , content: '/admin/books/addbooks'
                    , end: function () {
                        user.reload('user-table');

                    }
                });
            }
        };
        $('.userTable .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
        //-------会员管理表格e--------



    });
</script>

